package com.module.overview.ui.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.module.overview.R

class CustomCircleProgressBar @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private var roundColor: Int = Color.LTGRAY
    private var progressColor: Int = Color.BLUE
    private var roundWidth: Float = 10f
    private var max: Int = 100
    private var progress: Int = 0
    private var durationSeconds: Int = 10
    private var onProgressCompleteListener: (() -> Unit)? = null
    var isRunning = false
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val handler = Handler(Looper.getMainLooper())
    private var lastRunnable: Runnable? = null

    init {
        // 支持xml属性扩展
        attrs?.let {
            val ta = context.obtainStyledAttributes(it, R.styleable.overview_RoundProgressBar)
            roundColor = ta.getColor(R.styleable.overview_RoundProgressBar_overview_rpb_roundColor, roundColor)
            progressColor = ta.getColor(R.styleable.overview_RoundProgressBar_overview_rpb_roundProgressColor, progressColor)
            roundWidth = ta.getDimension(R.styleable.overview_RoundProgressBar_overview_rpb_roundWidth, roundWidth)
            ta.recycle()
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val centre = width / 2f
        val radius = centre - roundWidth / 2f
        val oval = RectF(centre - radius, centre - radius, centre + radius, centre + radius)

        // 画背景圆环
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = roundWidth
        paint.color = roundColor
        paint.strokeCap = Paint.Cap.ROUND
        canvas.drawCircle(centre, centre, radius, paint)

        // 画进度弧
        paint.color = progressColor
        val sweepAngle = 360f * progress / max
        canvas.drawArc(oval, -90f, sweepAngle, false, paint)
    }

    @Synchronized
    fun setProgress(progress: Int) {
        this.progress = progress.coerceIn(0, max)
        invalidate()
    }

    @Synchronized
    fun getProgress(): Int = progress

    fun setRoundColor(color: Int) {
        roundColor = color
        invalidate()
    }

    fun setProgressColor(color: Int) {
        progressColor = color
        invalidate()
    }

    fun setRoundWidth(width: Float) {
        roundWidth = width
        invalidate()
    }

    fun setDurationSeconds(seconds: Int) {
        durationSeconds = seconds
    }

    fun setOnProgressCompleteListener(listener: (() -> Unit)?) {
        onProgressCompleteListener = listener
    }

    fun startProgress() {
//        if (isRunning) return
        stopProgress() // 保证重入安全
        isRunning = true
        progress = 0
        invalidate()
        val totalSteps = max
        val interval = (durationSeconds * 1000L) / totalSteps
        lastRunnable = object : Runnable {
            override fun run() {
                if (!isRunning) return
                if (progress < max) {
                    progress++
                    invalidate()
                    handler.postDelayed(this, interval)
                } else {
                    isRunning = false
                    onProgressCompleteListener?.invoke()
                }
            }
        }
        handler.post(lastRunnable!!)
    }

    fun stopProgress() {
        isRunning = false
        lastRunnable?.let { handler.removeCallbacks(it) }
        lastRunnable = null
        progress = 0
        invalidate()
    }

    fun restartProgress() {
        stopProgress()
        startProgress()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        stopProgress()
    }
} 